<?php
class Category_model extends CI_Model
{
    public $tab_cate = 'store_category';
    public $tab_prod_cate = 'store_product_category';
    public $tab_product ='store_product';

    function getAll()
    {
        return $this->db
            ->get($this->tab_cate)
            ->result();
    }

    function getByUrl($category_url)
    {
    return $this->db
        ->where('category_url',$category_url)
        ->get($this->tab_cate)
        ->row();
    }
    
    function getOptions()
    {
        $result = $this->db->get($this->tab_cate)->result();
        $options = array();
        foreach ($result as $category)
        {
            $options[$category->category_id] = $category->category_name;//them ptu vao mang
        }
        return $options;//
    }
    function getName($category_id)
    {
        return $this->db
            ->select('category_name')
            ->where('category_id',$category_id)->get($this->tab_cate)->row();
    }
    function getOne($category_id)
    {
        return $this->db->where('category_id',$category_id)->get($this->tab_cate)->row();
    }

    function save(array $data,$category_id)
    {
        if($category_id === null)
        {
            $this->db->insert($this->tab_cate,$data);
        }
        else
        {
            $this->db->update($this->tab_cate,$data,array('category_id' =>$category_id));
        }
    }

    function delete($category_id)
    {
        $data=$this->db->join($this->tab_prod_cate,"$this->tab_prod_cate.product_id = $this->tab_product.product_id")
            ->where("$this->tab_prod_cate.category_id",$category_id)->get($this->tab_product)->result();
        //$data=$data['0']->product_id;
        foreach ($data as $product)
        {
            $product_id[] = $product->product_id;
            //sau mỗi lần lặp ta thêm từng product_id vào mảng $product_id[]
            //nếu ta khai báo
            //$product_id = array (
            //    'product_id' =>$product->product_id;
            //);
            //sau mỗi lần lập mảng $product_id chỉ lấy 1 product_id,kết quả cuối cùng là product ci cuối
        }
        if(isset($product_id))//neu ton tai product id thi xoa tat product
        {
            $this->db
                ->where_in('product_id',$product_id)
                ->delete($this->tab_product);
        }
        
        return $this->db
            ->delete($this->tab_cate,array('category_id' =>$category_id));
            //$this->db->delete('store_product_category',array('category_id'=>$category_id));
    }
    function update_item_status( $category_id, $category_active ) {
        $data = array (
            'category_active'	=> (int)$category_active
        );

        $this->db->where( 'category_id', (int)$category_id );
        $this->db->update( $this->tab_cate, $data );
    }
    function getRules()
    {
        return array(
            array(
                'field'=>'category_name',//ten truong trong data
                'label'=>'category name',//nhan vo van
                'rules'=>'trim|required'//bat buoc
            ),  
            array(
                'field'=>'category_url',//ten truong trong data
                'label'=>'category url',//nhan vo van
                'rules'=>'trim|required'//bat buoc
            ),  
            array(
                'field'=>'category_description',//ten truong trong data
                'label'=>'category description',//nhan vo van
                'rules'=>'trim|required'//bat buoc
            ),  
            array(
                'field'=>'category_meta_description',//ten truong trong data
                'label'=>'category meta description',//nhan vo van
                'rules'=>'trim|required'//bat buoc
            ),
        );
    }
}
?>